<template>
    <!-- 新增服务类型 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" align-center width="30%" :before-close="handleClose">
        <el-form :model="formdata" label-width="27%">
            <el-form-item label="服务类型名称">
                <el-input v-model="formdata.serviceName" style="width:  70%;" />
            </el-form-item>
            <el-form-item label="活动图片" label-width="27%" v-if="dialogTitle == '编辑服务子类别' || dialogTitle == '新增服务子类别'">
                <el-upload class="avatar-uploader" name="imageFile" :action="action" :show-file-list="false"
                    :on-success="handleImageSuccess" :before-upload="beforeAvatarUpload">
                    <el-image v-if="formdata.image" :src="formdata.image" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon">
                        <Plus />
                    </el-icon>
                </el-upload>
            </el-form-item>
            <el-form-item label="状态">
                <el-radio-group v-model="formdata.state">
                    <el-radio :value="0">禁用</el-radio>
                    <el-radio :value="1">启用</el-radio>
                </el-radio-group>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="cancelDialog">取消</el-button>
                <el-button type="primary" @click="toSave">提交</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import { Plus } from '@element-plus/icons-vue'
import { storeToRefs } from "pinia";
import { useServicetypeStore } from "../../../stores/servicetype.js";
const servicetypeStore = useServicetypeStore()
const { formdata, dialogTitle, dialogVisible } = storeToRefs(servicetypeStore)

const action = 'http://127.0.0.1:8083/upload/image'
const handleImageSuccess = (res) => {
    console.log(res)
    servicetypeStore.formdata.image = res.result
}

const beforeAvatarUpload = (res) => {
    console.log(res);
}

const cancelDialog = () => {
    servicetypeStore.dialogVisible = false
    servicetypeStore.dialogTitle = ''
    servicetypeStore.formdata = {}
}
const handleClose = () => {
    servicetypeStore.dialogVisible = false
    servicetypeStore.dialogTitle = ''
    servicetypeStore.formdata = {}
}

const toSave = () => {
    servicetypeStore.save()
}
</script>

<style scoped>
.avatar-uploader {
    width: 70%;
    /* height: 70%; */
    display: block;
}

.avatar {
    width: 100%;
}

.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>